<?php 
$req_path="./";
require_once($req_path."include/db.php");

class M_card extends DB{
	private $dbConn;
	private $rowCount="0";
	private $ID="0";
	private $sqlSelect ="select * from tb_user where 1=1";
	
	function M_user(){
		parent::__construct();
	}
	function getText(){
		echo "TEST";
	}
	
	function getData($name=null, $position=null, $group=null, $branch=null){
		$sqlscript= "Select u.UserID, u.UserName , u.FirstName, u.LastName, p.PositionName Position "
				." , b.BranchName Branch , g.GroupName Groupo , CASE WHEN u.Status =1 THEN 'Actived' ELSE 'Disabled' END Status "
				." From tb_user u "
				." LEFT join tb_position p on p.ID=u.Position "
				." LEFT join tb_branch b on b.ID=u.Branch "
				." LEFT join tb_usergroup g on g.GroupID=u.Group "
				." Where 1=1 ";
		
		if (isset($position) && $position!=""){
			$sqlscript.= " and Position=". $position;
		}
		if (isset($name) && $name!=""){
			$sqlscript.= " and FirstName Like '%". $name."%'";
		}
		if (isset($group) && $group !=""){
			$sqlscript.= " and Group = ". $group."";
		}
		if (isset($branch) && $branch !=""){
			$sqlscript.= " and Branch = ". $branch."";
		}
		$result = $this->ExecuteData($sqlscript);
		$this->rowCount =$result->RecordCount();
		return $result;
	}
	
	function getDataDetail($id=0){
		$sqlscript= "select UserID, UserName , Password , FirstName, LastName, Position, Branch,  Group, Status ";
		$sqlscript.= " from tb_user where 1=1 ";
		$sqlscript.= " and UserID='". $id."'";
		$result = $this->ExecuteData($sqlscript);
		return $result->FetchRow();
	}
	
	function getRowCount(){
		return $this->rowCount;
	}
	
	function getLastInsert(){
		return $this->ID;
	}
	
	function InsertData($val){
		if (isset($val)){
			$sqlInsert=$this->InsertSqlScript($this->sqlSelect, $val);
			$this->ExecuteData($sqlInsert);
			$this->ID = $this->getLastID();
			return $this->ID;
		}
	}
	function updateData($sql, $val){
		if(isset($val)){
			$sqlUpdate=$this->UpdateSqlScript($sql,$val);
			return $this->ExecuteData($sqlUpdate);
		}
	}
	function delData($id){
		if(isset($id)){
			$sqlDelete = "delete from tb_user where UserID='".$id."'";
			$this->ExecuteData($sqlDelete);
			return "c";
		}
	}
	function getDataCBO($t="b"){
		if ($t=='b')
			$sqlscript= "select ID as ID, BranchName as Code from tb_branch where IsActived=1 ";
		else if ($t=='g')
			$sqlscript= "select GroupID as ID, GroupName as Code from tb_usergroup where 1=1 ";
		else if ($t='p')	
			$sqlscript= "select ID as ID, PositionName as Code from tb_position where 1=1 ";
				
		$result = $this->ExecuteData($sqlscript);
		
		 while($row = $result->FetchRow()){
			$ar[$row["ID"]] = $row["Code"];
		 }
		 
		return $ar;
	}
}
?>